<?php 
class ControllerPageContact extends FrontController {
		
  	public function index() {

    	$this->document->setTitle($this->_['heading_title']);  
	 
    	if ($this->request->isPost() && $this->validate()) {
			$mail = new Mail();
			$mail->protocol = C('config_mail_protocol');
			$mail->parameter = C('config_mail_parameter');
			$mail->hostname = C('config_smtp_host');
			$mail->username = C('config_smtp_username');
			$mail->password = C('config_smtp_password');
			$mail->port = C('config_smtp_port');
			$mail->timeout = C('config_smtp_timeout');				
			$mail->setTo(C('config_email'));
	  		$mail->setFrom($this->request->post['email']);
	  		$mail->setSender($this->request->post['name']);
	  		$mail->setSubject(v5_html_entity_decode(sprintf($this->_['email_subject'], $this->request->post['name'])));
	  		$mail->setText(strip_tags(v5_html_entity_decode($this->request->post['enquiry'])));
      		$mail->send();

	  		$this->redirect(U('page/contact/success'));
    	}

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['heading_title'],
			'href'      => U('page/contact'),
        	'separator' => $this->_['text_separator']
      	);
    	
		if (isset($this->request->post['name'])) {
			$this->data['name'] = $this->request->post['name'];
		} else {
			$this->data['name'] = $this->customer->getFirstName();
		}

		if (isset($this->request->post['email'])) {
			$this->data['email'] = $this->request->post['email'];
		} else {
			$this->data['email'] = $this->customer->getEmail();
		}
		
		$this->data['enquiry'] = P('enquiry');
		$this->data['captcha'] = P('captcha');
		
 		$this->display('page/contact.tpl');		
  	}

  	public function success() {

		$this->document->setTitle($this->_['heading_title']); 

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['heading_title'],
			'href'      => U('page/contact'),
        	'separator' => $this->_['text_separator']
      	);	
		
    	$this->data['heading_title'] = $this->_['heading_title'];
    	$this->data['text_message'] = $this->_['text_message'];
		$this->data['continue'] = HTTP_SERVER;
		
 		$this->display('common/success.tpl'); 
	}
	
  	protected function validate() {
		$error = false;
    	if (!range_length($this->request->post['name'], 3, 32)) {
      		$this->setMessage('error_name', $this->_['error_name']);
			$error = true;
    	}

    	if (!preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
      		$this->setMessage('error_email', $this->_['error_email']);
			$error = true;
    	}

    	if (!range_length($this->request->post['enquiry'], 10, 1000)) {
      		$this->setMessage('error_enquiry', $this->_['error_enquiry']);
			$error = true;
    	}

    	if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
      		$this->setMessage('captcha', $this->_['error_captcha']);
			$error = true;
    	}
		return !$error;
  	}

	public function captcha() {
		$this->load->library('captcha');		
		$captcha = new Captcha();		
		$this->session->data['captcha'] = $captcha->getCode();		
		$captcha->showImage();
	}	
}
?>
